package view.editorActions;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.swt.widgets.Text;

import singleton.Singleton;
import swtElements.MyText;
import view.views.CSADView;
import view.views.TabEntrys;
import controller.UseCaseController;
import controller.data.EditDocumentationRevision;
import controller.data.createNewDocumentation.CreateNewDocumentationRequest;
import controller.data.editDocumentation.EditDocumentationRequest;
import controller.data.readDocumentation.ReadDocumentationRequest;

public class SaveDocumentation {

	public static void saveDocumentation(MyText textFirstName, MyText textLastName, MyText textEdit, MyText textTitle, MyText textTags, int selIndex){
		EditDocumentationRevision revision = new EditDocumentationRevision(textLastName.getText(), textFirstName
				.getText(), textEdit.getText(), textTitle.getText());
		
		String id = Singleton.getInstance().getUseCaseController().getId();
		
		String element = Singleton.getInstance().getSearchString();
		TabEntrys tabETMP = Singleton.getInstance().getTabentrys();
		String category = tabETMP.getEntrys(selIndex).getCategoryName();
		if (id == null) {
			// textTags = setTagsToSave(textTags);
			List<String> tags = new ArrayList<String>(Arrays.asList(textTags.getText()
					.split(";")));
			tags.add(category);
			tags.add(element);
			tags.add(textTitle.getText());
			
			CreateNewDocumentationRequest request = new CreateNewDocumentationRequest(
					revision, element, category, tags);
			String response = Singleton.getInstance().getUseCaseController()
					.createNewDocumentation(request);
			Singleton.getInstance().setResponse(response);
			Singleton.getInstance().useCaseController.setId(response);
		} else {
			// textTags = setTagsToSave(textTags);
			List<String> tags = new ArrayList<String>(Arrays.asList(textTags.getText()
					.split(";")));
			tags.add(category);
			tags.add(element);
			tags.add(textTitle.getText());
			EditDocumentationRequest request = new EditDocumentationRequest(id,
					revision, tags);
			String response = Singleton.getInstance().getUseCaseController()
					.editDocumentation(request);

			Singleton.getInstance().setResponse(response);
		}
		UseCaseController useTMP = Singleton.getInstance()
		.getUseCaseController();
		CSADView.listOfData = CSADView.listOfData = Singleton.getInstance()
		.getUseCaseController().readDocumentation(
				new ReadDocumentationRequest(Singleton.getInstance()
						.getSelectedTab().getCategoryName(),
						CSADView.searchString));
	}
}
